package com.example.everying.service.system.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.context.UserContext;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.mapper.system.SysNotificationMapper;
import com.example.everying.model.dto.system.SysNotificationDto;
import com.example.everying.model.entity.system.SysNotification;
import com.example.everying.model.vo.SysNotificationVo;
import com.example.everying.model.vo.user.UserInfoVo;
import com.example.everying.service.system.SysNotificationService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMapper, SysNotification> implements SysNotificationService {
    @Autowired
    private WebSocketChatService webSocketChatService;

    @Override
    public List<SysNotification> listByTypeAndStatus(Byte type, Byte status) {
        return query().eq("type", type).eq("status", status).list();
    }

    @Override
    public boolean saveNotification(SysNotification notification) {
        notification.setCreateTime(LocalDateTime.now());
        notification.setUpdateTime(LocalDateTime.now());
        return save(notification);
    }

    @Override
    public boolean updateNotificationById(SysNotification notification) {
        notification.setUpdateTime(LocalDateTime.now());
        return updateById(notification);
    }

    @Override
    public boolean deleteNotificationById(Long id) {
        SysNotification notification = getById(id);
        if (notification != null) {
            notification.setIsDelete(true);
        }
        return updateById(notification);
    }

    /** 批量删除已读的通知
     * @return
     */
    @Override
    public boolean deleteNotificationBatch() {
        UserInfoVo userInfoVo = UserContext.getUser();
        UpdateWrapper<SysNotification> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_delete", true);
        updateWrapper.eq("recipient_id", userInfoVo.getId());
        updateWrapper.eq("msg_status", "1");
        updateWrapper.eq("is_delete", false);
        return update(updateWrapper);
    }

    /**
     * @param pageParam
     * @param dto
     * @return
     */
    @Override
    public IPage<SysNotification> selectPage(Page<SysNotification> pageParam, SysNotificationDto dto) {
        QueryWrapper<SysNotification> queryWrapper = new QueryWrapper<>();
        if (dto.getTitle() != null && !dto.getTitle().isEmpty()){
            queryWrapper.like("title", dto.getTitle());
        }
        queryWrapper.eq("is_delete",0);
        queryWrapper.orderByDesc("create_time");

        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.between("create_time", dto.getStartTime(), dto.getEndTime());
        }
        queryWrapper.eq("recipient_id",dto.getRecipientId());
        return baseMapper.selectPage(pageParam, queryWrapper);
    }



}
